package com.smartthinking.CICPNL;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.Window;

import com.smartthinking.CICPNL.home.activity.ImageFragment;
import com.smartthinking.CICPNL.home.activity.Splash2;
import com.smartthinking.CICPNL.home.activity.Utilities;

public class EncuestasPager extends FragmentActivity {
    private MyAdapter mAdapter;
    private ViewPager mPager;
	public String qry_local;
	public ArrayList<Dictionary<String, String>> TraeDatos;
	public static ArrayList<Dictionary<String,String>> Splash = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_activity_encuestas_pager);
		
		int EVEIDR = Utilities.EVE_IDR;
		
		//splash
		try{
			String qry = "select a.* from eventos_banners a inner join banners_catalogo b on a.ban_idr = b.ban_idr where a.eve_idr = "+EVEIDR+" and b.ban_nombre = 'SPLASH ENCUESTAS' ORDER BY b.ban_nombre";
			Splash = Utilities.qry2(getApplicationContext(), qry);
					
			if(Splash.size()>0){
				Dictionary<String, String> dic = Splash.get(0);
				String strPoster = dic.get("ban_url").toString();
				String strTimer = dic.get("ban_timer").toString();
				String strPatrocinador = dic.get("pat_idr").toString();
				if (strPoster.equals("null")) {
					strPoster = "";
				}
				if(strPoster.length()>0){
					Intent intent = new Intent(this, Splash2.class);
					Bundle bund = new Bundle();
					bund.putString("PUT_IMG",strPoster);
					bund.putString("PUT_PAT",strPatrocinador);
					bund.putString("PUT_TIME",strTimer);
					intent.putExtras(bund);
					startActivity(intent);
				}
			}
		}catch(Exception ex){}

		new CargarEncuestas().execute();
	}
	
	public void voidFinish(View V) {
		finish();
	}
	
	public class CargarEncuestas extends AsyncTask<Void, Void, Void>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			qry_local = "select * from encuestas where eve_idr="+Utilities.EVE_IDR+" order by enc_idr";
			TraeDatos = Utilities.qry2(getApplicationContext(), qry_local);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			mAdapter = new MyAdapter(getSupportFragmentManager(), TraeDatos);
	        mPager = (ViewPager) findViewById(R.id.pager);
	        mPager.setAdapter(mAdapter);
		}
	}


	public static class MyAdapter extends FragmentPagerAdapter {
		ArrayList<Dictionary<String, String>> TraeDatos;
		public MyAdapter(FragmentManager fm, ArrayList<Dictionary<String, String>> TraeDatos) {
			super(fm);
			this.TraeDatos = TraeDatos;
		}

		@Override
		public int getCount() {
			return TraeDatos.size();
		}

		@Override
		public Fragment getItem(int position) {
			Dictionary<String, String> actualdic = TraeDatos.get(position);
			return new EncuestaFragment(actualdic, position);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ui_activity_encuestas_pager, menu);
		return true;
	}

}
